package com.damie.web.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.damie.common.commonutils.R;
import com.damie.common.dao.WasteOrder;
import com.damie.common.dto.WasteOrderDto;
import com.damie.common.paramer.WasteOrderParamer;
import com.damie.web.service.UserService;
import com.damie.web.service.WasteOrderService;
import com.damie.web.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/waste-order")
@Slf4j
@Api(value="订单",tags={"站点接单管理"})
public class WasteOrderController {
    @Autowired
    private WasteOrderService wasteOrderService;
    @Autowired
    private UserService userService;



    /**
     * 查看所有订单接口
     * @param wasteOrderDto
     * @return
     */
    @ApiOperation("查看所有未接单的订单")
    @PostMapping("findAll")
    public R findAll(@RequestBody WasteOrderDto wasteOrderDto){
        WasteOrderParamer wasteOrderParamer = new WasteOrderParamer();
        BeanUtils.copyProperties(wasteOrderDto,wasteOrderParamer);
        log.info("查看所有订单{}",wasteOrderDto);
        List<WasteOrder> all = wasteOrderService.findAll(wasteOrderParamer);
        System.out.println(all);
        return R.ok().data("all",all);
    }


    /**
     * 查看单个订单详情界面
     * @param wasteOrderId
     * @return
     */
    @ApiOperation("查看订单详情")
    @GetMapping("findOne/{wasteOrderId}")
    public R findOne(@PathVariable("wasteOrderId") String wasteOrderId){
//        http://localhost:5000/waste-order/findOne/1353529172401139714
        log.info("查看订单详情{}",wasteOrderId);
        OrderDetails one = wasteOrderService.findOne(wasteOrderId);
        return R.ok().message("查询成功").data("one1",one);
    }

    /**
     * 分页查看订单接口
     * @param currentPage
     * @param size
     * @param wasteOrderDto
     * @return
     */
    @ApiOperation("分页查询订单")
    @PostMapping("page/{currentPage}/{size}")
    public R getPage(@PathVariable("currentPage") Integer currentPage,@PathVariable("size") Integer size,@RequestBody WasteOrderDto wasteOrderDto){
        WasteOrderParamer wasteOrderParamer = new WasteOrderParamer();
        BeanUtils.copyProperties(wasteOrderDto,wasteOrderParamer);
        log.info("分页查看订单{}",wasteOrderDto);
        Page<WasteOrder> wasteOrderPage = new Page<>(currentPage, size);
        IPage<WasteOrder> iPage = wasteOrderService.getPageVo(wasteOrderPage, wasteOrderParamer);
        return R.ok().message("分页查询成功").data("all",iPage);
    }

//    @ApiOperation("分页查询订单")
//    @PostMapping("page")
//    public R getPage(Integer currentPage,Integer size,@RequestBody WasteOrderDto wasteOrderDto){
//        WasteOrderParamer wasteOrderParamer = new WasteOrderParamer();
//        BeanUtils.copyProperties(wasteOrderDto,wasteOrderParamer);
//        log.info("分页查看订单{}",wasteOrderDto);
//        Page<WasteOrder> wasteOrderPage = new Page<>(currentPage, size);
//        IPage<WasteOrder> iPage = wasteOrderService.getPageVo(wasteOrderPage, wasteOrderParamer);
//        return R.ok().message("分页查询成功").data("all",iPage);
//    }

    @ApiOperation("分页查看本站点未完成订单")
    @PostMapping("page1/{currentPage1}/{size1}")
    public R unFinish(@PathVariable("currentPage1") Integer currentPage, @PathVariable("size1") Integer size, @RequestBody MyOrder myOrder, HttpServletRequest httpServletRequest){
        log.info("分页查看订单{}",myOrder);
        Page<MyOrder> wasteOrderPage = new Page<>(currentPage, size);
        IPage<MyOrder> iPage = wasteOrderService.unFinish(wasteOrderPage, myOrder,httpServletRequest);
        return R.ok().message("分页查询成功").data("unFinished",iPage);
    }

    @ApiOperation("分页查看本站点已完成订单")
    @PostMapping("page2/{currentPage2}/{size2}")
    public R finish(@PathVariable("currentPage2") Integer currentPage, @PathVariable("size2") Integer size, @RequestBody MyOrder myOrder, HttpServletRequest httpServletRequest){
        log.info("分页查看订单{}",myOrder);
        Page<MyOrder> wasteOrderPage = new Page<>(currentPage, size);
        IPage<MyOrder> iPage = wasteOrderService.finish(wasteOrderPage, myOrder,httpServletRequest);
        return R.ok().message("分页查询成功").data("finished",iPage);
    }

    @ApiOperation("分页查看本站点已取消订单")
    @PostMapping("page3/{currentPage3}/{size3}")
    public R cancel(@PathVariable("currentPage3") Integer currentPage, @PathVariable("size3") Integer size, @RequestBody MyOrder myOrder, HttpServletRequest httpServletRequest){
        log.info("分页查看订单{}",myOrder);
        Page<MyOrder> wasteOrderPage = new Page<>(currentPage, size);
        IPage<MyOrder> iPage = wasteOrderService.cancel(wasteOrderPage, myOrder,httpServletRequest);
        return R.ok().message("分页查询成功").data("canceled",iPage);
    }


    /**
     * 站点接单并修改订单状态
     * @param wasteOrderDto
     * @param lockKey
     * @return
     */
    @ApiOperation("接单修改订单状态,并且上锁")
    @PostMapping("orderTaking")
    public R orderTaking(@RequestBody WasteOrderDto wasteOrderDto,String lockKey){
//        {
//            "wasteOrderId": "1353529172401139714",
//                "siteId": "1351422697177509890",
//                "wasteOrderStatus": 0
//        }
        lockKey="123";
        System.out.println(wasteOrderDto);
        WasteOrderParamer wasteOrderParamer = new WasteOrderParamer();
        BeanUtils.copyProperties(wasteOrderDto,wasteOrderParamer);
        String s = wasteOrderService.orderTaking(wasteOrderParamer,lockKey);
        return R.ok().message("开始接单").data("返回",s);
    }

    /**
     * 计算司机到用户的距离
     * @param wasteOrderId
     * @return
     */
    @ApiOperation("计算用户与站点距离")
    @PostMapping("findAddress/{wasteOrderId}")
    public R findAddress(@PathVariable("wasteOrderId") String wasteOrderId){
//        {
//            "wasteOrderId": "1351465434128019458,1351858028879101953"
//        }
        Distance address = wasteOrderService.findAddress(wasteOrderId);
        return R.ok().message("计算距离成功").data("distance",address);
    }

    @ApiOperation("修改订单价格,并完成订单,增加用户积分")
    @PostMapping("updateWasteOrder")
    public R updateWasteOrder(@RequestBody WasteOrderVo wasteOrderVo){
//        http://localhost:5000/waste-order/updateWasteOrder
//        {
//            "wasteOrderId": "1351465434128019458",
//                "wasteOrderStatus": 0,
//                "wasteOrderDetailsVos": [
//            {
//                "wasteOrderDetailsId": "1351790048245743618",
//                    "wasteNumber": 11,
//                    "wastePrice": 131.0
//            },
//            {
//                "wasteOrderDetailsId": "1351885381374148609",
//                    "wasteNumber": 12,
//                    "wastePrice": 132.0
//            }
//            ]
//        }
        int i = wasteOrderService.updateWasteOrder(wasteOrderVo);
        userService.addIntegral(wasteOrderVo.getWasteOrderId());
        return R.ok().message("修改成功").data("修改",i);
    }

    @ApiOperation("修改订单详情")
    @PostMapping("updateWasteOrderDetails1")
    public R updateWasteOrderDetails1(@RequestBody WasteOrderDetailsVo wasteOrderDetailsVo){
        int i = wasteOrderService.updateWasteOrderDetails1(wasteOrderDetailsVo);
        return R.ok().message("修改成功").data("upd",i);
    }

    @ApiOperation("修改订单状态增加积分")
    @PostMapping("updateWasteOrder1")
    public R updateWasteOrder(@RequestBody WasteOrderVo1 wasteOrderVo1){
        int i = wasteOrderService.updateWasteOrder1(wasteOrderVo1);
        userService.addIntegral(wasteOrderVo1.getWasteOrderId());
        return R.ok().message("修改成功").data("upd1",i);
    }


    /**
     * 订单完成,增加用户积分
     */
    @ApiOperation("积分")
    @PostMapping("addIntegral")
    public R addIntegral(String wasteOrderId){
        userService.addIntegral(wasteOrderId);
        return R.ok().message("增加成功");
    }
}

